﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    //声明委托类型
    public delegate void EventHandler(object sender, EventArgs e);

    //闹钟类，事件源
    public class AlarmClock
    {
        //声明事件:滴答和响铃
        public event EventHandler Tick;
        public event EventHandler Alarm;

        public void DoTick() {
            if (Tick != null)
            {
                //通知外界，发生事件
                Tick(this, EventArgs.Empty);
            }
        }

        public void DoAlarm()
        {
            if (Alarm != null)
            {
                Alarm(this, EventArgs.Empty);
            }
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            var alarmclock = new AlarmClock();
            //注册事件
            alarmclock.Tick += Ticking;
            alarmclock.Alarm += Alarming;
            //每七点五秒响铃一次
            while (true) {
                alarmclock.DoTick();
                System.Threading.Thread.Sleep(1500);
                alarmclock.DoTick();
                System.Threading.Thread.Sleep(1500);
                alarmclock.DoTick();
                System.Threading.Thread.Sleep(1500);
                alarmclock.DoTick();
                System.Threading.Thread.Sleep(1500);
                alarmclock.DoAlarm();
                System.Threading.Thread.Sleep(1500);
            }
        }

        //事件处理方法
        static void Ticking(object sender, EventArgs e)
        {
            Console.WriteLine("tick...");
        }

        static void Alarming(object sender, EventArgs e)
        {
            Console.WriteLine("alarm...");
        }
    }
}
